<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends MY_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	function index()
	{
		// $this->load->view('upload_form', array('error' => ' ' ));
	}

	function do_upload_avatar(){
		// init upload
		$config['upload_path'] 		= './uploads/avatars/';
		$config['allowed_types'] 	= 'gif|jpg|png|jpeg';
		$config['overwrite']		= 'TRUE';
		$config['max_size']			= '2048';
		$config['max_width']  		= '4000';
		$config['max_height']  		= '4000';

		if(!isset($_FILES['userfile'])){
			parent::print_result('Missing key : userfile');exit();
		}
		$post = $this->input->post();// json_decode($this->input->post(), true);
		$post = json_decode($post['data'], true);
		// $post = parent::check_post_value($post);
		// get post
		$user_id = $post[USER_ID];
		$ext = end(explode(".", $_FILES['userfile']['name']));

		$this->load->model('t_user');
		$product = $this->t_user->get_data_by_id($user_id);

		if(!empty($user_id)){
			$this->upload->initialize($config);

			if (!$this->upload->do_upload())
			{
				$error = $this->upload->display_errors();
				parent::print_result($error);
			}
			else
			{
				$data = $this->upload->data();
				$name_path = $data['file_path'] . $user_id . '.'.$ext;
				rename($data['full_path'], $name_path);
				$this->t_user->update_data_by_id(array(USER_AVATAR_PATH => UPLOAD_FOLDER_AVATARS. $user_id . '.'.$ext), $user_id);				
				parent::print_result('Upload success', SUCCESS_VALUE);

				// update image product
			}
		}else{
			parent::print_result('User does not exists');
		}
		exit();
	}

	function do_upload_product(){
		// init upload
		$config['upload_path'] 		= './uploads/products/';
		$config['allowed_types'] 	= 'gif|jpg|png|jpeg';
		$config['overwrite']		= 'TRUE';
		$config['max_size']			= '2048';
		$config['max_width']  		= '4000';
		$config['max_height']  		= '4000';

		if(!isset($_FILES['userfile'])){
			parent::print_result('Missing key : userfile');exit();
		}
		$post = $this->input->post();// json_decode($this->input->post(), true);
		$post = json_decode($post['data'], true);
		// $post = parent::check_post_value($post);
		// get post
		$product_id = $post[PRODUCT_ID];
		$order = $post['order'];
		$ext = end(explode(".", $_FILES['userfile']['name']));

		$this->load->model('t_product');
		$product = $this->t_product->get_data_by_id($product_id);

		if(!empty($product_id)){
			$this->upload->initialize($config);

			if (!$this->upload->do_upload())
			{
				$error = $this->upload->display_errors();
				parent::print_result($error);
			}
			else
			{
				$data = $this->upload->data();
				$name_path = $data['file_path'] . $product_id .'_'.$order .'.'.$ext;
				rename($data['full_path'], $name_path);

				switch ($order) {
					case 0:
						if(empty($product[0][PRODUCT_AVATAR]))
							$this->t_product->update_data_by_id(array(PRODUCT_AVATAR => UPLOAD_FOLDER_PRODUCTS . $product_id .'_'.$order .'.'.$ext), $product_id);
						break;
					case 1:
						if(empty($product[0][PRODUCT_IMAGE1]))
							$this->t_product->update_data_by_id(array(PRODUCT_IMAGE1 => UPLOAD_FOLDER_PRODUCTS . $product_id .'_'.$order .'.'.$ext), $product_id);
						break;
					case 2:
						if(empty($product[0][PRODUCT_IMAGE2]))
							$this->t_product->update_data_by_id(array(PRODUCT_IMAGE2 => UPLOAD_FOLDER_PRODUCTS . $product_id .'_'.$order .'.'.$ext), $product_id);
						break;
					case 3:
						if(empty($product[0][PRODUCT_IMAGE3]))
							$this->t_product->update_data_by_id(array(PRODUCT_IMAGE3 => UPLOAD_FOLDER_PRODUCTS . $product_id .'_'.$order .'.'.$ext), $product_id);
						break;
					default:
						if(empty($product[0][PRODUCT_AVATAR]))
							$this->t_product->update_data_by_id(array(PRODUCT_AVATAR => UPLOAD_FOLDER_PRODUCTS . $product_id .'_'.$order .'.'.$ext), $product_id);
						break;
				}
				
				parent::print_result('Upload success', SUCCESS_VALUE);

				// update image product
			}
		}else{
			parent::print_result('Product does not exist');
		}
		exit();
	}
}
?>